package com.demo.demo4j;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.util.List;

public class jaxenXpath {
    public static void main(String[] args) {
        jaxenXpath testor = new jaxenXpath();
//		testor.xpath("/hr/employee");
//		testor.xpath("//employee");
		testor.xpath("//employee[salary<4000]");
//		testor.xpath("//employee[name='李铁柱']");
//		testor.xpath("//employee[@no=3304]");//通过属性筛选
//		testor.xpath("//employee[1]");//通过位置获取
//		testor.xpath("//employee[last()]");、、获取最后一个
        //testor.xpath("//employee[position()<3]");//当前位置position  当前位置小于3
//        testor.xpath("//employee[3] | //employee[8]");//获取第三个和获取第八个
    }

    public void xpath(String xpathExp) {
        String file = "E:\\DemoStudy\\Java\\2020\\12\\2020webDemoAll\\xmlDemo\\src\\main\\java\\xml\\hr.xml";
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(file);
            List<Node> nodes = document.selectNodes(xpathExp);
            for (Node node : nodes) {
                Element emp = (Element) node;
                System.out.println(emp.attributeValue("no"));
                System.out.println(emp.elementText("name"));
                System.out.println(emp.elementText("age"));
                System.out.println(emp.elementText("salary"));
                System.out.println("==============================");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
